package thread2;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @time: 2019/11/22 13:57
 * @author: baozi
 */
public class threadDemo2 implements Runnable {

    private static Integer num=30;
    private final Integer cc=20;
    Lock lock=new ReentrantLock();

    @Override
    public void run() {
        for(;1<2;){
            //ss();
            /*synchronized (cc){
                if(num>0){
                    System.out.println(Thread.currentThread().getName()+"减少"+num);
                    num--;
                }
            }*/
            lock.lock();
            if(num>0){
                System.out.println(Thread.currentThread().getName()+"减少"+num);
                num--;
            }
            lock.unlock();
        }
    }

    /*public static *//*synchronized*//* void ss(){
        synchronized (threadDemo2.class){
            if(num>0){
                System.out.println(Thread.currentThread().getName()+"减少"+num);
                num--;
            }
        }
    }*/

    public static void main(String[] args) {
        threadDemo2 demo2=new threadDemo2();
        Thread th=new Thread(demo2);
        Thread th1=new Thread(demo2);
        Thread th2=new Thread(demo2);
        th.start();
        th1.start();
        th2.start();


        //System.out.println(Thread.currentThread().getName()+"22222222222");

      /* new threadDemo1(){
           @Override
           public void run(){
               System.out.println(Thread.currentThread().getName()+"3333333");
           }
       }.start();*/


       /* threadDemo2 streamDemo1=new threadDemo2(){
           @Override
           public void run(){
               System.out.println(Thread.currentThread().getName()+"4444");
           }
       };
       // streamDemo1.run();
        Thread th=new Thread(streamDemo1);
        th.start();*/
    }


}
